﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.WebApp.ViewModel;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.Infrastructure.Authentication;


namespace XSTDZ.ERP.WebApp.Controllers
{
    //产品类别
    public class ProductTypeController : Controller
    {
        private readonly IProductService productTypeServiceImpl = ServiceLocator.Instance.GetService<IProductService>();
        //
        // GET: /ProductType/

        public ActionResult Index()
        {
           // var productTypeRoots = productTypeServiceImpl.GetAllProductType(0);//层级为0的所有根本结点
           //var nodes= productTypeRoots.OrderBy(p=>p.TypeOrder).Select(p => new OrgViewModel { id=p.ID.ToString(), text=p.TypeName, icon="" });

           return View();
        }
        [NoAuthorizated("BaseProductTypes")]
        public ActionResult BaseProductTypes()
        {
            return View();
        }
         [NoAuthorizated("SelectProductTypes")]
        public ActionResult SelectProductTypes()
        {
            return View();
        } 
        #region 添加
        [HttpGet]
        [NoAuthorizated("Add")]
        public ActionResult Add(Guid? id)
        {
            //Guid parentId;
            //if (id != null && id.HasValue)
            //{
            //    Guid.TryParse(id.ToString(), out parentId);
            //    var parentType = productTypeServiceImpl.GetProductType(parentId);
            //    ViewBag.ParentTypeNode = parentType;
            //}
            return View();
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(ProductTypeDto productTypeDto)
        {
            if (ModelState.IsValid)
            {
                productTypeServiceImpl.AddProductType(productTypeDto);
                ViewBag.PostState = 9;
            }
            return View();
        }
        #endregion
        #region 修改
        [HttpGet]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(Guid id)
        {
            var productType = productTypeServiceImpl.GetProductType(id);
            return View(productType);
        }
        [HttpPost]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(ProductTypeDto productTypeDto)
        {
            if (ModelState.IsValid)
            {
                productTypeServiceImpl.UpdateProductType(productTypeDto);
                ViewBag.PostState = 9;
            }
            return View();
        }
        #endregion
        #region 删除
         [HttpGet]
         [NoAuthorizated("Delete")]
        public ActionResult Delete(Guid id)
        {
            productTypeServiceImpl.ConvertProductTypeDeleteState(id);
            return Content("ok");
        }
        #endregion
        #region 获得产品类型列表
         [NoAuthorizated("GetJsonOfProductType")]
        public JsonResult GetJsonOfProductType(Guid? parentId,bool isRoot)
        {
            Guid parentProductID;
            IList<ProductTypeDto> productTypes=null;

            var model = new ProductTypeModel();
            IList<OrgViewModel> jsonResults = null;
            if (isRoot)
            {
                if (parentId == null || !Guid.TryParse(parentId.ToString(), out parentProductID))
                {
                    productTypes = productTypeServiceImpl.GetAllProductType(0).Where(p => p.Deleted == 0).ToList();

                }
            }
            else
            {
                Guid.TryParse(parentId.ToString(), out parentProductID);
                productTypes = productTypeServiceImpl.GetAllProductType(parentProductID).Where(p => p.Deleted == 0).ToList();
            }
            jsonResults = model.GetProductTypes(productTypes);
            return Json(jsonResults, JsonRequestBehavior.AllowGet); 
        }
        #endregion
    }
}
